<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible"/>
    <title>老寇IoT云平台OAuth2统一认证</title>
	<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<script type="text/javascript">

(function(){
    function OAuth2(url){
        this.url = url;
        this.code = null;
        this.accessToken = null;
        this.tokenType = null;
        this.state = null;
        //缓存在localStorage中的对象
        this.cacheValue = null;
    }
    OAuth2.prototype.init = function(){
        const local = this.url
        this.code = this.getKey("code",local,"")
        this.accessToken = this.getKey("access_token",local,"")
        this.tokenType = this.getKey("token_type",local,"Bearer")
        this.state = this.getKey("state",local)
        if(window.localStorage){
            const value = window.localStorage.getItem(this.state)
            if(this.strNotBlank(value)){
                this.cacheValue=JSON.parse(value)
            }
        }
    }
    OAuth2.prototype.auth = function(){
        if(this.strNotBlank(this.code)){
            this.authorizationCode()
        }else{
            this.implicit()
        }
    }
    OAuth2.prototype.getKey=function(key,str,defaultValue){
        const reg = new RegExp(".*?" + key + "=(.*?)(&.*)?$", "ig")
        let val = defaultValue
        if(reg.test(str)){
            val = RegExp.$1
        }
        return val
    }
    OAuth2.prototype.strNotBlank = function(str) {
        let flag = false
        if (str !== undefined &&str !== "") {
            flag = true
        }
        return flag
    }
    OAuth2.prototype.implicit = function(){
		console.log(this.cacheValue)
        this.cacheValue.accessToken = this.tokenType + " " + this.accessToken
        this.cacheValue.tokenType = this.tokenType
        this.cacheValue.granted = true
        window.localStorage.setItem(this.state,JSON.stringify(this.cacheValue))
        window.close()
    }
    OAuth2.prototype.authorizationCode = function(){
        const that = this;
        const url = this.cacheValue.tokenUrl;
        const params = {
            "grant_type": "authorization_code",
            "code": this.code,
            "redirect_uri": decodeURIComponent(this.cacheValue.redirectUri)
        };
        let instance = axios.create();
        let requestConfig = {
            url: url,
            method: 'post',
            timeout: 0,
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
				Authorization: 'Basic ' + btoa(this.cacheValue.clientId + ":" + this.cacheValue.clientSecret)
			},
            data: params
        }
        instance.request(requestConfig).then(res => {
            let data = res.data
            if(data != null) {
                that.cacheValue.accessToken = data.token_type + " " + data.access_token
                that.cacheValue.tokenType = data.token_type
                that.cacheValue.granted = true
                window.localStorage.setItem(that.state,JSON.stringify(that.cacheValue))
                window.close()
            }
        })
    }
    const oauth = new OAuth2(window.location.href)
    oauth.init()
    oauth.auth()
})()
</script>
</body>
</html>
